<?php
namespace Common;
use Common\Models\FUsersExtend;
use Common\Models\User;
use Library\Tools\JwtTools;

class ApiController extends BaseController
{
    public $user_info = array();

    public function initialize()
    {
        parent::initialize();

//        if (!strstr($this->params['_url'], 'login2') && !strstr($this->params['_url'], 'ordersync') && !strstr($this->params['_url'], 'test') && !strstr($this->params['_url'], 'goods') && !strstr($this->params['_url'], 'home') && !strstr($this->params['_url'], 'product')) {
//            $jwtData = (new JwtTools())->verifyToken($this->header['Token'], 'app');
//            if ($jwtData === false) {
//                $jwtData['sub'] = 1;
//                //$this->fail(-1, '登录已失效,请重新登录');
//            }
//        }

        $jwtData['sub'] = 2;

        if($jwtData['sub']){
            $user = new User();
            $this->user_info = $user->get_info($jwtData['sub']);
            if (!$this->user_info) {
                $this->fail(-200, '用户不存在-200');
            }
            if (intval($this->user_info['is_disable']) == 1) {
                $this->fail(-1, '用户被禁止');
            }
            $this->params['user_id'] = $jwtData['sub'];
            //用户信息
            $this->find_id_update_time($jwtData['sub']);
        }

    }


    //修改用户的最后登录时间
    public function find_id_update_time($user_id)
    {
        $user = new User();
        $user_update = $user->find_by_id($user_id);
        if ($user_update) {
            if ($user_update->is_appuser == 0) {
                $user_update->is_appuser = 1;
            }
            $user_update->updated_at = date('Y-m-d H:i:s');
            $user_update->save();
        }

        $FUserExtendsDB = new FUsersExtend();
        $fuserExtends = $FUserExtendsDB->findFirstByUserId($user_id);
        if(!$fuserExtends){
            $FUserExtendsDB->user_id = $user_id;
            $FUserExtendsDB->save();
        }
    }

    //通用佣金算法 还需要修改
    public function money_less($res, $site = '')
    {
        foreach ($res as $k => $v) {
            $commission = Common::commission($this->user_info['id'], $v['Commission']);
            $res[$k]['GoodsId'] = isset($v['GoodsId']) && !empty($v['GoodsId']) ? (string)$v['GoodsId'] : '';
            $v['Title'] = "　  {$v['Title']}";  //解决前端不能输入空格的原因
            $res[$k]['Title'] = isset($v['Title']) && !empty($v['Title']) ? (string)$v['Title'] : '';
            $res[$k]['Icon'] = 'http://qn.enshitc.com/1668006796193_ic_search_logo_taobao.png';
            $v['ShortTitle'] = "　　{$v['ShortTitle']}";  //解决前端不能输入空格的原因
            $res[$k]['ShortTitle'] = isset($v['ShortTitle']) && !empty($v['ShortTitle']) ? (string)$v['ShortTitle'] : '';
            $res[$k]['ShopType'] = isset($v['ShopType']) && !empty($v['ShopType']) ? (string)$v['ShopType'] : '';
            $res[$k]['PicLogo'] = strpos($v['PicLogo'], 'ttp') ? (string)$v['PicLogo'] : 'https:' . $v['PicLogo'];
            $res[$k]['CouponMoney'] = isset($v['CouponMoney']) && !empty($v['CouponMoney']) ? Common::bc($v['CouponMoney'],"0",'+',2) : '0';
            $res[$k]['Price'] = isset($v['Price']) && !empty($v['Price']) ? $v['Price'] : '0';
            $res[$k]['EndPrice'] = isset($v['EndPrice']) && !empty($v['EndPrice']) ? Common::bc($v['EndPrice'],'0','+',2) : '0';
            $res[$k]['MonthSale'] = isset($v['MonthSale']) && !empty($v['MonthSale']) ? (string)$v['MonthSale'] : '0';
            $res[$k]['Site'] = isset($v['Site']) && !empty($v['Site']) ? (string)$v['Site'] : '';
            $res[$k]['ShopName'] = isset($v['ShopName']) && !empty($v['ShopName']) ? (string)$v['ShopName'] : '';
            $res[$k]['IsAd'] = isset($v['IsAd']) && !empty($v['IsAd']) ? (int)$v['IsAd'] : 0;
            $res[$k]['CouponTotal'] = isset($v['CouponTotal']) && !empty($v['CouponTotal']) ? (int)$v['CouponTotal'] : '0';
            $res[$k]['VideoUrl'] = isset($v['VideoUrl']) && !empty($v['VideoUrl']) ? (string)$v['VideoUrl'] : '';
            $res[$k]['CouponSurplus'] = isset($v['CouponSurplus']) && !empty($v['CouponSurplus']) ? (int)$v['CouponSurplus'] : '0';
            $res[$k]['Commission'] = Common::bc($commission['my_commisson'],'0','+',2);
            $res[$k]['Couponurl'] = isset($v['Couponurl']) && !empty($v['Couponurl']) ? (string)$v['Couponurl'] : '';
            $res[$k]['MaxCommission'] = Common::bc($commission['max_commisson'],'0','+','2');
            $res[$k]['ItemFrom'] = isset($v['ItemFrom']) && !empty($v['ItemFrom']) ? (string)$v['ItemFrom'] : '';
        }
        return $res;
    }

}
